package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.MxbSort;
import com.gx.mxbbx.service.MxbSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class MxbSortController {
    @Autowired
    private MxbSortService mxbSortService;

    @RequestMapping("/getMxbSortList")
    public String getMxbSortList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String sortName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<MxbSort> pageInfo = mxbSortService.getMxbSortList(sortName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", sortName);
        return "/mxbsort/mxbsort_list";
    }

    @RequestMapping("/openmxbsortAdd")
    public String openmxbsortAdd(Model model) {
        return "/mxbsort/mxbsort_add";
    }

    @RequestMapping("/mxbsortadd")
    public String mxbsortadd(MxbSort mxbSort, HttpServletRequest request, Model model) {
        mxbSortService.mxbsortadd(mxbSort);
        return getMxbSortList(1, 10, request, model);
    }

    @RequestMapping("/mxbsortadddelete")
    public String mxbsortadddelete(Integer id, HttpServletRequest request, Model model) {
        mxbSortService.mxbsortadddelete(id);
        return getMxbSortList(1, 10, request, model);
    }

    @RequestMapping("/openmxbsortEdit")
    public String openmxbsortEdit(Integer id, Model model) {
        MxbSort mxbSort = mxbSortService.openmxbsortEdit(id);
        model.addAttribute("mxbSort", mxbSort);
        return "/mxbsort/mxbsort_edit";
    }

    @RequestMapping("/mxbsortedit")
    public String mxbsortedit(MxbSort mxbSort, HttpServletRequest request, Model model) {
        mxbSortService.mxbsortedit(mxbSort);
        return getMxbSortList(1, 10, request, model);
    }
}
